package com.luo.ams.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luo.ams.service.IPermissionService;
import com.luo.common.restful.JsonResult;
import com.luo.pojo.ams.dto.PermissionAddDTO;
import com.luo.pojo.ams.entity.Permission;
import com.luo.pojo.ams.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author bin.cn
 * @since 2023-01-19
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult getPermissionList(){
        List<Permission> list = permissionService.list(new QueryWrapper<Permission>().orderByAsc("sort"));
        return JsonResult.ok(list);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult add(PermissionAddDTO permissionAddDTO){
        permissionService.add(permissionAddDTO);
        return JsonResult.ok();
    }

    @GetMapping("/detail/{id}")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult getDetail(@PathVariable Integer id){
        Permission permission = permissionService.getById(id);
        return JsonResult.ok(permission);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult update(Permission permission){
        permissionService.updateById(permission);
        return JsonResult.ok();
    }

    /**
     * 删除权限
     */
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('/admin/manage')")
    public JsonResult deleteById(@PathVariable Long id){
        permissionService.deleteById(id);
        return JsonResult.ok();
    }
}
